var app = getApp()
Page({
  data: {
    current:0,
    showCardIntroduce: false,  //显示会员卡权益和使用说明
    currentCard:{},
	orderfenlei:'all_0',
    payType: '1',
    hadCardRight:true,
    hadCardNotice: true,
    page: 0,
	level_id:0,
    noMoretip: false,
	sellevel_id:0,
	levelnum:0,
	payprice:0,
	memberShop:[],
	selectIds:[],
	levelInfo:[]
  },
  onLoad: function (e) {
    var that = this;
    app.setVersion(that);
    wx.getSystemInfo({
      success: function(res) {
        console.log(res)
        var x = res.windowWidth - 80;
        var y = res.windowHeight - 70;
        that.setData({
          x: x,
          y: y,
          showBtn:true
        })
      },
    })
  },
  onShow: function () {
	  var that=this;
	  that.requestMember();
  },
  chooseFenlei: function (e) {
	console.log(e)
    var that = this;
    that.setData({
      memberShop: null,
      classstyle: 'active',
	  current:e.currentTarget.dataset.index,
	  level_id:e.currentTarget.dataset.id,
      noMoretip: false,
      showLoading: true
    })
	that.data.memCard.forEach(function(ite){
		if(ite.mul_id == e.currentTarget.dataset.id){
			that.setData({
			  payprice: ite.mul_buy_money,
			})
		}
	})
    that.requestMember(e.currentTarget.dataset.id);
  },
  openMemberLevelPay:function(e){
  	  var that=this;
  	  var data = {};
  	  data.map = 'applet_pay_member_userlevel';
  	  data.level_id = that.data.level_id;
  	  console.log(data);
  	  if(data.level_id==0){
  		  wx.showToast({
  			icon: 'none',
  			title: '请选择专区类型',
  		  })
  		  return;
  	  }
  	  wx.showToast({
  	    title: '加载中',
  	    icon: 'loading',
  	    mask: true,
  	    duration: 1000
  	  });
  	  wx.request({
  	    url: app.globalData.requestUrl,
  	    data: data,
  	    success: function (res) {
  			if (res.data.ec == 200) {
  			  console.log(res.data.data);
  			  wx.requestPayment({
  			    'appId': res.data.data.appId,
  			    'timeStamp': res.data.data.timeStamp,
  			    'nonceStr': res.data.data.nonceStr,
  			    'package': res.data.data.package,
  			    'signType': res.data.data.signType,
  			    'paySign': res.data.data.paySign,
  			      success: function (res) {
  			      wx.showToast({
  			        icon: 'success',
  			        title: '支付成功',
  			        success() {
  			          setTimeout(function () {
  			            wx.navigateBack({
  			              delta: 1
  			            })
  			          }, 1500)
  			        },
  			      })
  			    },
  			    fail: function (res) {
  			      wx.showToast({
  			        icon: 'none',
  			        title: '支付失败',
  			      })
  			      console.log(res); 
  			    }
  			  });
  			} else {
  			  wx.showToast({
  			    icon: 'none',
  			    title: res.data.em,
  			  })			  
  			}
  		},
  	 })
  },
  openMemberLevelPay:function(e){
	  var that=this;
	  var data = {};
	  data.map = 'applet_pay_member_userlevel';
	  data.level_id = that.data.level_id;
	  console.log(data);
	  if(data.level_id==0){
		  wx.showToast({
			icon: 'none',
			title: '请选择专区类型',
		  })
		  return;
	  }
	  //发起请求，获取列表列表
	  wx.showToast({
	    title: '加载中',
	    icon: 'loading',
	    mask: true,
	    duration: 1000
	  });
	  wx.request({
	    url: app.globalData.requestUrl,
	    data: data,
	    success: function (res) {
			if (res.data.ec == 200) {
			  console.log(res.data.data);
			  wx.requestPayment({
			    'appId': res.data.data.appId,
			    'timeStamp': res.data.data.timeStamp,
			    'nonceStr': res.data.data.nonceStr,
			    'package': res.data.data.package,
			    'signType': res.data.data.signType,
			    'paySign': res.data.data.paySign,
			      success: function (res) {
			      wx.showToast({
			        icon: 'success',
			        title: '支付成功',
			        success() {
			          setTimeout(function () {
			            wx.navigateBack({
			              delta: 1
			            })
			          }, 1500)
			        },
			      })
			    },
			    fail: function (res) {
			      wx.showToast({
			        icon: 'none',
			        title: '支付失败',
			      })
			      console.log(res); 
			    }
			  });
			} else {
			  wx.showToast({
			    icon: 'none',
			    title: res.data.em,
			  })			  
			}
		},
	 })
  },
  // 新接口获取专区等级以及等级商品
  requestMember: function (level_id) {
    var that = this;
    var data = {};
    data.map = 'applet_userlevel_list';	
	data.level_id =level_id;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(res);
        if (res.data.ec == 200) {
          var memCard = res.data.data;
		  var levelInfo =res.data.levelInfo;
          that.setData({
            memCard: memCard, 
			levelInfo:levelInfo,
			level_id:levelInfo.mul_id,
			payprice:levelInfo.mul_buy_money
          })
			
        }
      },
      complete: function () {
        wx.hideLoading();
        wx.stopPullDownRefresh();
      }
    })
  }
})